package com.ruoyi.ring.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.http.HttpRequest;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.ring.domain.RingVoiceRecord;
import com.ruoyi.ring.domain.VoiceTranModel;
import com.ruoyi.ring.service.IRingVoiceRecordService;
import com.ruoyi.ring.service.IVoiceTranslatService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.FileSystemResource;

public class VoiceTranslatWhisperServiceImpl implements IVoiceTranslatService {

    private static final Logger logger = LoggerFactory.getLogger(VoiceTranslatWhisperServiceImpl.class);

    private IRingVoiceRecordService voiceRecordService = SpringUtils.getBean(IRingVoiceRecordService.class);

    @Override
    public VoiceTranModel voiceTranslat(String voicePath) {
        return null;
    }

    @Override
    public AjaxResult voiceTranslat(RingVoiceRecord voiceRecord) {

        try {
            /**
             * 转写服务器信息
             */
            HttpRequest request = new HttpRequest();
            String result = request.sendFile("http://218.17.184.249:55000/transcribe", new FileSystemResource(voiceRecord.getVoicePath().replace(Constants.RESOURCE_PREFIX, RuoYiConfig.getProfile())));
            logger.info("Voice记录id【"+voiceRecord.getId()+"】，转写结果："+result);
            VoiceTranModel voiceTranModel = JSONObject.parseObject(result, VoiceTranModel.class);
            voiceRecordService.tranFinishHandle(voiceRecord, voiceTranModel);
            return AjaxResult.success();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error();
        }
    }

    @Override
    public VoiceTranModel textTranVoice(String content, String voicePath) {
        return null;
    }

    @Override
    public AjaxResult realVoiceTranslat() {
        return null;
    }

}
